#include <Wt/WTabWidget>
#include <Wt/WText>
#include <Wt/WVBoxLayout>
#include <Wt/WStackedWidget>
#include <Wt/WPushButton>
#include <Wt/WMenu>
#include <Wt/WApplication>
#include <Wt/WEnvironment>
#include <Wt/WTable>

#include "CommonWidget.h"
#include "PersonalWidget.h"
#include "OtherWidget.h"

CommonWidget::CommonWidget(int id,WApplication *app):
        WContainerWidget(),UserId(id),app1(app)
   {

       this->setId("CommonWidget");
       tabWidget = new WTabWidget(this);
       tabWidget->setId("tabWidget");
       myPage = new PersonalWidget(id, app1);
       myPage->setId("myPage");
       otherPage = new OtherWidget(id, app1);
       otherPage->setId("otherPage");

       //WTable *table = new WTable(this);
       //logout = new WText("We can try to use this tab to logout, can't we?"); //, table->elementAt(0,0));

       //logoutBtn= new WPushButton("Log OUT");
       logoutCont=new WContainerWidget();
       //logoutCont->addWidget(logout);
      // logoutCont->addWidget(logoutBtn);
       tabWidget->addTab(myPage, "My page");
       tabWidget->addTab(otherPage, "Other pages");
       tabWidget->addTab(logoutCont,"Logout");
tabWidget->currentChanged().connect(this,&CommonWidget::logOutFunc);
      // logoutBtn->clicked().connect(this, &CommonWidget::logOutFunc);
       //logoutCont->clicked().connect(this, &CommonWidget::logOutFunc);

       //contents = new WStackedWidget();
       //menu = new WMenu(contents, Wt::Vertical);
       //menu->setRenderAsList(true);
       //menu->addItem("My page", myPage);
       //menu->addItem("Other pages", otherPage);
       //menu->addItem("Logout", logout);

       //WVBoxLayout *layout = new WVBoxLayout();
       //layout->addWidget(tabWidget);
       //layout->addWidget(menu);
       //layout->addWidget(contents);
       //this->setLayout(layout, Wt::AlignCenter);

       //tabWidget->setCurrentWidget(myPage);
}

void CommonWidget::logOutFunc(int tab){
if (tab==2)
    {
    if (app1->instance()->environment().supportsCookies()==true){
      std::string strName="userid";
      std::string strValue=QString::number(0).toStdString();
      app1->instance()->setCookie(strName,strValue,1000);
    }

    app1->finalize();
    app1->redirect(app1->instance()->environment().internalPath()+"dietsystem");
     }
}

CommonWidget::~CommonWidget()
{
    delete myPage;
    delete otherPage;
    delete logoutCont;
    delete tabWidget;

}

